package com.dao;

import com.DBM.DBManager;
import com.entity.Dto;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017-02-09.
 */
public class FindDao extends DBManager{
    public List<Dto> laodAllCake(Dto dto,int page, int displaycount) throws SQLException{
        List<Dto> dtoList = new ArrayList<Dto>();
        Connection conn = this.openconnection();
        String sql = "\tSELECT * FROM goods \n" +
                "\tLEFT JOIN goodsattribute\n" +
                "\tON goods.goodsId=goodsAttribute.goodsAttributeId\n" +
                "\tLEFT JOIN photo\n" +
                "ON goods.goodsId=photo.goodsId WHERE goods.goodsName like \"%" + dto.getGoodsName() + "%\" limit ?,?";
        Object[] objects = {((page-1)*displaycount),displaycount};
        System.out.println("objects的长度"+objects.length);
        ResultSet rs = this.query(conn,sql,objects);
        Dto u = null;
        if(rs.next()){
            u = new Dto();
            u.setGoodsName(rs.getString("goodsName"));
            u.setGoodsId(rs.getInt("goodsId"));
            u.setPhotoId(rs.getInt("photoId"));
            u.setPhotoUrl(rs.getString("photoUrl"));
            u.setGoodsName(rs.getString("goodsName"));
            u.setGoodsInfo(rs.getString("goodsInfo"));
            u.setDisCount(rs.getInt("discount"));
            u.setPrice(rs.getDouble("price"));
            u.setGoodsSize(rs.getInt("goodsSize"));
            dtoList.add(u);
        }
        this.closeconnection(conn);
        return dtoList;
    }
    public int getCount(Dto dto) throws SQLException{
        int Count = 0;
        String sql = "SELECT Count(*) FROM goods WHERE goodsName like \"%"+dto.getGoodsName()+"%\"";
        Connection conn = this.openconnection();
//        Object object[] = {dto.getGoodsName()};
                ResultSet rs = this.query(conn,sql,null);
        if(rs.next()){
            Count = rs.getInt(1);
        }
        this.closeconnection(conn);
        return Count;
    }
}
